const TokenKey = 'ACCESS-TOKEN'

const payment = {}

payment.alipay = function(){
	uni.getProvider({
		service: 'payment',
		success: (res) => {
			//先判断用户是否有支付宝支付环境
			if (~res.provider.indexOf('alipay')) { 
				uni.showLoading({title: '正在调起支付宝支付'})
				//给后端传什么参数看你的后端需要什么
				let params = {
					ispc:3
				}
				uni.request({
					url: `${this.$baseUrl}/api-order/amstc/userRechargeAccountByAliPay`,
					
					method: 'POST',
					
					header: {
					
					"Token":this.userToken,
					
					"Content-Type":"application/x-www-form-urlencoded"
					
					},
					data:params,
					success:function(result){
						if(res.data.code==200){
							let payInfo=res.data.data  //拿到后端返回数据后调用下面支付函数
							uni.requestPayment({	
								provider: 'alipay',								
								orderInfo: payInfo, //支付宝订单数据（string类型）
								success:function(_res){
									uni.hideLoading();									
									uni.showToast({title: '支付成功',icon:'none'})
								},
								fail:function(_res){
									uni.hideLoading();									
									uni.showToast({title: '支付失败，稍后重试',icon:'none'})
								}
							})
							
						}
					},
					fail:function(_res){						
						
						uni.hideLoading();
						
						uni.showToast({title: '服务器开小差了呢，请您稍后再试',icon:'none'})
					}
				})
			}else{
				uni.showToast({title: '获取支付宝通道失败，请检查您的支付宝是否正常启用',icon:'none'})
			}
		}
	})
}
payment.weixin = function(token){
	uni.getProvider({
		service: 'payment',
		success: (res) => {
			//先判断用户是否有支付宝支付环境
			if (~res.provider.indexOf('wxpay')) { 
				uni.showLoading({title: '正在调起支付宝支付'})
				//给后端传什么参数看你的后端需要什么
				let params = {
					ispc:3
				}
				uni.request({
					url: `${this.$baseUrl}/api-order/amstc/userRechargeAccountByAliPay`,
					
					method: 'POST',
					
					header: {
					
					"Token":this.userToken,
					
					"Content-Type":"application/x-www-form-urlencoded"
					
					},
					data:params,
					success:function(result){
						if(res.data.code==200){
							let resobj=res.data.data  //拿到后端返回数据后调用下面支付函数
	
							let payInfo={
							
								appid: resobj.appid,
								
								noncestr: resobj.nonce_str,
								
								package:"Sign=WXPay",
								
								partnerid: resobj.mch_id,
								
								prepayid: resobj.prepay_id,
								
								timestamp: resobj.time_stamp,
								
								sign: resobj.sign,
							
							}
							uni.requestPayment({	
								provider: 'wxpay',								
								orderInfo: payInfo, //支付宝订单数据（string类型）
								success:function(_res){
									uni.hideLoading();									
									uni.showToast({title: '支付成功',icon:'none'})
								},
								fail:function(_res){
									uni.hideLoading();									
									uni.showToast({title: '支付失败，稍后重试',icon:'none'})
								}
							})
							
						}
					},
					fail:function(_res){						
						
						uni.hideLoading();
						
						uni.showToast({title: '服务器开小差了呢，请您稍后再试',icon:'none'})
					}
				})
			}else{
				uni.showToast({title: '获取支付宝通道失败，请检查您的支付宝是否正常启用',icon:'none'})
			}
		}
	})
}


export default payment
